package com.avecsys.quotemanager.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

import com.avecsys.quotemanager.utils.MessageBoardUtil;

/**
 * 
 * @author sam sundar K
 * 
 */
public class ValidatorEmail extends BaseValidator {

	private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\."
			+ "[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*"
			+ "(\\.[A-Za-z]{2,})$";

	public FacesMessage validateFieldValue(FacesContext facesContext,
			UIComponent uIComponent, Object object) throws ValidatorException {
		String emailInputField = (String) object;
		FacesMessage message = new FacesMessage();
		Pattern emailPattern = Pattern.compile(EMAIL_PATTERN);
		Matcher emailMatcher = emailPattern.matcher(emailInputField);
		if (!emailMatcher.matches()) {
			message.setSummary(MessageBoardUtil.getValue(facesContext,
					"commonMsgs", "VALIDATION_INVALID_EMAIL_ADDRESS", null));
		}
		return message;
	}
}
